package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

@Service
public class IBankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService{
    /*
     * 银行账户业务实现
     * */
    @Override
    public void upsert(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        bankAccountUpsertReqDTO.setId(currentUserInfo.getId());
        bankAccountUpsertReqDTO.setType(currentUserInfo.getUserType());
        //转换
        BankAccount bankAccount = BeanUtil.toBean(bankAccountUpsertReqDTO, BankAccount.class);
        //调用父类的saveOrUpdate方法更新bankAccount
        super.saveOrUpdate(bankAccount);

    }

    @Override
    public BankAccountResDTO currentUserBankAccount(Integer type) {
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = lambdaQuery()
                .eq(BankAccount::getId, userId)
                .one();
        BankAccountResDTO bankAccountResDTO = BeanUtil.toBean(bankAccount,BankAccountResDTO.class);
        bankAccountResDTO.setId(userId);
        bankAccountResDTO.setType(type);
        return bankAccountResDTO;
    }


}
